package com.ruoyi.system.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.Cars;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QuestionsMapper;
import com.ruoyi.system.domain.Questions;
import com.ruoyi.system.service.IQuestionsService;

/**
 * 提问Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class QuestionsServiceImpl implements IQuestionsService {
    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 查询提问
     *
     * @param id 提问主键
     * @return 提问
     */
    @Override
    public Questions selectQuestionsById(Long id) {
        Questions questions = questionsMapper.selectQuestionsById(id);
        questions.setQuestionsImages(imagesMapper.selectImages("question"+questions.getId()));
        return questions;
    }

    /**
     * 查询提问列表
     *
     * @param questions 提问
     * @return 提问
     */
    @Override
    public List<Questions> selectQuestionsList(Questions questions) {
        List<Questions> questionsList = questionsMapper.selectQuestionsList(questions);

        return questionsList.stream().peek(o -> {
            List<String> strings = imagesMapper.selectImages("question" + o.getId());
            o.setQuestionsImages(strings);
        }).collect(Collectors.toList());
    }
    /**
     * 新增提问
     *
     * @param questions 提问
     * @return 结果
     */
    @Override
    public int insertQuestions(Questions questions) {
        int insertQuestions = questionsMapper.insertQuestions(questions);
        //新增汽车成功后通过信息查询出这个汽车的id
        List<Questions> questionsList = questionsMapper.selectQuestionsList(questions);
        //拿到汽车id
        Long aLong = questionsList.get(0).getId();

        List<String> questionsImages = questions.getQuestionsImages();
        //利用stream 流 将拿到的图片名称都放进images 关联表中
        questionsImages.stream().map(map -> imagesMapper.insertSelective(new Images(null, "question" + aLong, map))).collect(Collectors.toList());

        return insertQuestions;
    }

    /**
     * 修改提问
     *
     * @param questions 提问
     * @return 结果
     */
    @Override
    public int updateQuestions(Questions questions) {
        int updateQuestions = questionsMapper.updateQuestions(questions);
        imagesMapper.deleteByForeginId("question"+questions.getId());
        //去重
        Set<String> strings = new HashSet<>(questions.getQuestionsImages());

        // 添加
        strings.forEach(o -> imagesMapper.insert(new Images(null,"question"+questions.getId(),o)));

        return updateQuestions;
    }

    /**
     * 批量删除提问
     *
     * @param ids 需要删除的提问主键
     * @return 结果
     */
    @Override
    public int deleteQuestionsByIds(Long[] ids) {
        return questionsMapper.deleteQuestionsByIds(ids);
    }

    /**
     * 删除提问信息
     *
     * @param id 提问主键
     * @return 结果
     */
    @Override
    public int deleteQuestionsById(Long id) {
        return questionsMapper.deleteQuestionsById(id);
    }
}
